package com.clip.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

public class LongweiboUtils {
	private final Font CONTENTFONT=new Font("微软雅黑",Font.PLAIN,24);
	private final Font TITLEFONT=new Font("微软雅黑",Font.BOLD,32);
	
	private final int PADDINGTOP=50;
	private final int CONTENTSPAN=30;
	
	private BufferedImage tmpImage;
	private Graphics2D tmpgraphics;
	
	private BufferedImage image;
	private int contentfontHeight;
	private int titlefontHeight;
	private int sumheight=0;
	private int graphicsimageheight=0;
	private int ccnamewidth=0;
	private int imagewidth;
	private int imageheight;
	private List<String> ccnames;
	private List<String> titles;
	private List<String> times;
	private List<String> addresses;
	private List<String> deadlinetimes;
	private List<String> details;
	
	private File imagefile;
	
	
	private URL bottomurl;
	public LongweiboUtils() {
		createTmpGraphics2D();
		bottomurl=this.getClass().getClassLoader().getResource("clipdimension.jpg");
		ccnames=new ArrayList<String>();
		titles=new ArrayList<String>();
		times=new ArrayList<String>();
		addresses=new ArrayList<String>();
		deadlinetimes=new ArrayList<String>();
		details=new ArrayList<String>();
	}
	private void createTmpGraphics2D(){
		int imageWidth = 640;// 图片的宽度，固定的宽度
		int imageHeight = 400;// 图片的高度，临时的高度
		
		tmpImage = new BufferedImage(imageWidth, imageHeight,
				BufferedImage.TYPE_INT_RGB);
		tmpgraphics = tmpImage.createGraphics();
		
		FontMetrics fm = tmpgraphics.getFontMetrics(TITLEFONT);
		titlefontHeight= fm.getHeight(); 
		fm = tmpgraphics.getFontMetrics(CONTENTFONT);
		contentfontHeight= fm.getHeight();
		
		this.sumheight+=PADDINGTOP;
	}
	public List<String> getCcnames() {
		return ccnames;
	}
	public void setCcnames(List<String> ccnames) {
		this.ccnames = ccnames;
	}
	public List<String> getTitles() {
		return titles;
	}
	public void setTitles(List<String> titles) {
		this.titles = titles;
	}
	public List<String> getTimes() {
		return times;
	}
	public void setTimes(List<String> times) {
		this.times = times;
	}
	public List<String> getAddresses() {
		return addresses;
	}
	public void setAddresses(List<String> addresses) {
		this.addresses = addresses;
	}
	public List<String> getDeadlinetimes() {
		return deadlinetimes;
	}
	public void setDeadlinetimes(List<String> deadlinetimes) {
		this.deadlinetimes = deadlinetimes;
	}
	public List<String> getDetails() {
		return details;
	}
	public void setDetails(List<String> details) {
		this.details = details;
	}
	
	public void computeHeadHeight(String head){
		FontMetrics fm = tmpgraphics.getFontMetrics(this.TITLEFONT);
		int titlewidth=0;
		int rowIndex=1;
		StringBuffer sb=new StringBuffer();
		for(int i=0;i<head.length();i++){
			char c = head.charAt(i);

			titlewidth+=fm.charWidth(c);
			if(titlewidth>600){
				rowIndex++;  
				this.ccnames.add(sb.toString());
				sb=new StringBuffer();
			}
			sb.append(c);
		}
		this.ccnames.add(sb.toString());
		this.sumheight+=rowIndex*this.titlefontHeight;
		this.ccnamewidth=titlewidth;
		
		this.sumheight+=PADDINGTOP;
	}
	
	public void computeContentHeight(String content,List<String> list){
		FontMetrics fm = tmpgraphics.getFontMetrics(this.CONTENTFONT);
		
		int titlewidth=0;
		int offsetLeft = 40;
		int rowIndex = 1;
		StringBuffer sb=new StringBuffer();
		for(int i=0;i<content.length();i++){
			char c = content.charAt(i);
			if(c=='\n'){
				rowIndex++;  
                offsetLeft = 40;
                list.add(sb.toString());
                sb=new StringBuffer();
                continue;
            }
			int charWidth = fm.charWidth(c); //字符的宽度  
            
            //另起一行  
            if(Character.isISOControl(c) || offsetLeft >= (610-charWidth)){  
                rowIndex++;  
                offsetLeft = 40;
                list.add(sb.toString());
                sb=new StringBuffer();
            }  
            sb.append(c);
//            graphics.drawString(String.valueOf(c), offsetLeft, rowIndex * fontHeight);  
            offsetLeft += charWidth;  
		}
		list.add(sb.toString());
		this.sumheight+=rowIndex * this.contentfontHeight;
		
		this.sumheight+=CONTENTSPAN;
//		System.out.println(this.sumheight);
	}
	
	public void computeImageHeight(String imageurl) throws IOException{
		File imagefile=new File(imageurl);
		BufferedImage img = ImageIO.read(imagefile);
		int imgwidth=img.getWidth();
		int imgheight=img.getHeight();
	
		if(imgwidth>600){
			this.imageheight=(int) ((imgheight * 600) / imgwidth);// (原始图高
			this.imagewidth=600;
		}else{
			this.imageheight=imgheight;
			this.imagewidth=imgwidth;
		}
		this.sumheight+=this.imageheight;
		this.imagefile=imagefile;
		this.sumheight+=CONTENTSPAN;
	}
	
	public void finishComputeImageHeight(){
		int l=new Double(Math.ceil(new Double(this.sumheight)/new Double(50))).intValue();
		this.graphicsimageheight=l*50;
		this.graphicsimageheight+=100;
		this.tmpgraphics=null;
		this.tmpImage=null;
	}
	
	public void createLongWeiImage(String generateImagepath){
		int imageWidth = 640;// 图片的宽度，固定的宽度
		int imageHeight = this.graphicsimageheight;// 图片的高度，临时的高度
		
		image= new BufferedImage(imageWidth, imageHeight,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D graphics = image.createGraphics();
		graphics.setColor(Color.white);
		graphics.fillRect(0, 0, imageWidth, imageHeight);
		graphics.setColor(new Color(0x444644));
		
		graphics.setFont(this.TITLEFONT);
		int posindex=addHead2Image(graphics);
		graphics.setFont(this.CONTENTFONT);
		posindex=addNewContent2Image(graphics,posindex,this.times);
		posindex=addNewContent2Image(graphics,posindex,this.titles);
		if(this.addresses.size()>0)
			posindex=addNewContent2Image(graphics,posindex,this.addresses);
		if(this.deadlinetimes.size()>0)
			posindex=addNewContent2Image(graphics,posindex,this.deadlinetimes);
		if(this.details.size()>0)
			posindex=addNewContent2Image(graphics,posindex,this.details);
		
		
		if(this.imagefile!=null){
			BufferedImage bimg = null;
			try {
				bimg = javax.imageio.ImageIO.read(imagefile);
				ScaleImage imagetools=new ScaleImage();
				bimg=imagetools.imageZoomOut(bimg, this.imagewidth, this.imageheight);
				
			} catch (Exception e) {
			}
			if (bimg != null)
				graphics.drawImage(bimg, (640-this.imagewidth)/2,posindex , null);
		}
		graphics.setColor(new Color(0xededed));
		graphics.fillRect(0, this.graphicsimageheight-100, imageWidth, 100);
		File bottomfile=new File(this.bottomurl.getPath());
		BufferedImage bimg = null;
		try {
			bimg = javax.imageio.ImageIO.read(bottomfile);
		} catch (Exception e) {
		}
		if (bimg != null)
			graphics.drawImage(bimg, 0,this.graphicsimageheight-100 , null);
		
		graphics.dispose();
		createImage(generateImagepath);
		
	
	}
	
	private int addHead2Image(Graphics2D graphics2d){
		int posindex=this.PADDINGTOP;
		if(this.ccnames.size()>0){
			int offsetLeft=0;
			if(this.ccnamewidth>=600){
				offsetLeft=20;
			}else{
				offsetLeft=(600-this.ccnamewidth)/2;
			}
			int rowIndex=0;
			for (String str : this.ccnames) {
				graphics2d.drawString(str, offsetLeft, posindex+rowIndex * this.titlefontHeight);
				rowIndex++;
			}
			
			posindex+=rowIndex * this.titlefontHeight;
			posindex+=this.PADDINGTOP;
		}
		return posindex;
	}
	
	private int addNewContent2Image(Graphics2D graphics2d,int posindex,List<String> list){
		int offsetLeft=40;
		int rowIndex=0;
		for (String str : list) {
			graphics2d.drawString(str, offsetLeft, posindex+rowIndex * this.contentfontHeight);
			rowIndex++;
		}
		posindex+=rowIndex * this.contentfontHeight;
		posindex+=this.CONTENTSPAN;
		return posindex;
	}
	
	private void createImage(String fileLocation) {
		try {
			FileOutputStream fos = new FileOutputStream(fileLocation);
			BufferedOutputStream bos = new BufferedOutputStream(fos);
			ImageIO.write(image, "GIF",bos);
			bos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
